package me.zhengjie.modules.serviceprovider.service.impl;

import me.zhengjie.modules.serviceprovider.domain.TServiceproviderOrder;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.modules.serviceprovider.repository.TServiceproviderOrderRepository;
import me.zhengjie.modules.serviceprovider.service.TServiceproviderOrderService;
import me.zhengjie.modules.serviceprovider.service.dto.TServiceproviderOrderDTO;
import me.zhengjie.modules.serviceprovider.service.dto.TServiceproviderOrderQueryCriteria;
import me.zhengjie.modules.serviceprovider.service.mapper.TServiceproviderOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

/**
* @author xuyukun
* @date 2019-08-25
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TServiceproviderOrderServiceImpl implements TServiceproviderOrderService {

    @Autowired
    private TServiceproviderOrderRepository tServiceproviderOrderRepository;

    @Autowired
    private TServiceproviderOrderMapper tServiceproviderOrderMapper;

    @Override
    public Object queryAll(TServiceproviderOrderQueryCriteria criteria, Pageable pageable){
        Page<TServiceproviderOrder> page = tServiceproviderOrderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(tServiceproviderOrderMapper::toDto));
    }

    @Override
    public Object queryAll(TServiceproviderOrderQueryCriteria criteria){
        return tServiceproviderOrderMapper.toDto(tServiceproviderOrderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public TServiceproviderOrderDTO findById(Integer id) {
        Optional<TServiceproviderOrder> tServiceproviderOrder = tServiceproviderOrderRepository.findById(id);
        ValidationUtil.isNull(tServiceproviderOrder,"TServiceproviderOrder","id",id);
        return tServiceproviderOrderMapper.toDto(tServiceproviderOrder.get());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TServiceproviderOrderDTO create(TServiceproviderOrder resources) {
        return tServiceproviderOrderMapper.toDto(tServiceproviderOrderRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TServiceproviderOrder resources) {
        Optional<TServiceproviderOrder> optionalTServiceproviderOrder = tServiceproviderOrderRepository.findById(resources.getId());
        ValidationUtil.isNull( optionalTServiceproviderOrder,"TServiceproviderOrder","id",resources.getId());

        TServiceproviderOrder tServiceproviderOrder = optionalTServiceproviderOrder.get();
        // 此处需自己修改
        resources.setId(tServiceproviderOrder.getId());
        tServiceproviderOrderRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        tServiceproviderOrderRepository.deleteById(id);
    }
}